/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pp.persistance;

import com.pp.entites.Hse;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.pp.entites.Site;
import com.pp.persistance.exceptions.NonexistentEntityException;
import com.pp.persistance.exceptions.PreexistingEntityException;
import com.pp.persistance.exceptions.RollbackFailureException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author arnaud
 */
public class HseJpaController implements Serializable {

    public HseJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Hse hse) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
//            utx.begin();
            em = getEntityManager();
            Site idsite = hse.getIdsite();
            if (idsite != null) {
                idsite = em.getReference(idsite.getClass(), idsite.getId());
                hse.setIdsite(idsite);
            }
            em.persist(hse);
            if (idsite != null) {
                idsite.getHseList().add(hse);
                idsite = em.merge(idsite);
            }
//            utx.commit();
        } catch (Exception ex) {
            try {
//                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findHse(hse.getId()) != null) {
                throw new PreexistingEntityException("Hse " + hse + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Hse hse) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
//            utx.begin();
            em = getEntityManager();
            Hse persistentHse = em.find(Hse.class, hse.getId());
            Site idsiteOld = persistentHse.getIdsite();
            Site idsiteNew = hse.getIdsite();
            if (idsiteNew != null) {
                idsiteNew = em.getReference(idsiteNew.getClass(), idsiteNew.getId());
                hse.setIdsite(idsiteNew);
            }
            hse = em.merge(hse);
            if (idsiteOld != null && !idsiteOld.equals(idsiteNew)) {
                idsiteOld.getHseList().remove(hse);
                idsiteOld = em.merge(idsiteOld);
            }
            if (idsiteNew != null && !idsiteNew.equals(idsiteOld)) {
                idsiteNew.getHseList().add(hse);
                idsiteNew = em.merge(idsiteNew);
            }
//            utx.commit();
        } catch (Exception ex) {
            try {
//                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = hse.getId();
                if (findHse(id) == null) {
                    throw new NonexistentEntityException("The hse with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
//            utx.begin();
            em = getEntityManager();
            Hse hse;
            try {
                hse = em.getReference(Hse.class, id);
                hse.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The hse with id " + id + " no longer exists.", enfe);
            }
            Site idsite = hse.getIdsite();
            if (idsite != null) {
                idsite.getHseList().remove(hse);
                idsite = em.merge(idsite);
            }
            em.remove(hse);
//            utx.commit();
        } catch (Exception ex) {
            try {
//                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Hse> findHseEntities() {
        return findHseEntities(true, -1, -1);
    }

    public List<Hse> findHseEntities(int maxResults, int firstResult) {
        return findHseEntities(false, maxResults, firstResult);
    }

    private List<Hse> findHseEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Hse.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Hse findHse(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Hse.class, id);
        } finally {
            em.close();
        }
    }

    public int getHseCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Hse> rt = cq.from(Hse.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
